package com.naizhuai.ui.msgmanager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.MultStateView;
import com.naizhuai.ui.components.swipeback.SwipeBackActivity;
import com.naizhuai.ui.entity.User.AppMessageList;
import com.naizhuai.ui.entity.User.CateGoryMsg;
import com.naizhuai.ui.msgmanager.multtype.MainFlowViewBinder;
import com.naizhuai.ui.msgmanager.multtype.ShowTimeViewBinder;
import com.naizhuai.ui.msgmanager.multtype.StackMailViewBinder;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.naizhuai.util.TimeUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class MsgNotifyActivity extends SwipeBackActivity {
    public static final String MESSAGECATEGORY = "messageCategory";
    @SuppressLint("SimpleDateFormat")
    private static final DateFormat _FORMAT = new SimpleDateFormat("MM/dd/HH:mm");
    @BindView(R.id.multstateView)
    MultStateView multstateView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    public static void start(Context context, String category) {
        Intent intent = new Intent(context, MsgNotifyActivity.class);
        intent.putExtra(MESSAGECATEGORY,category);
        if (context instanceof BaseActivity)
            ((BaseActivity) context).startActivityTransition(intent);
    }

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.msg_classify)
    RecyclerView msgClassify;
    MultiTypeAdapter mTypeAdapter;
    Items mItems;
    private String msgcategory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        toolbar.setNavigationOnClickListener(v -> finish());
        InitView();
        InitData();
    }

    private void InitView() {
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            if (!TextUtils.isEmpty(msgcategory)) {
                loadCataData(msgcategory);
            }
        });
        mTypeAdapter = new MultiTypeAdapter();

        mTypeAdapter.register(String.class, new ShowTimeViewBinder());

        mTypeAdapter.register(AppMessageList.class).to(
                new MainFlowViewBinder(),
                new StackMailViewBinder()
        ).withClassLinker((position, data) -> {
            if (MsgDataAdapter.STACK_MAIL.equals(msgcategory)) {
                return StackMailViewBinder.class;
            } else {
                return MainFlowViewBinder.class;
            }
        });

        msgClassify.setLayoutManager(new LinearLayoutManager(this));

        mTypeAdapter.setItems(mItems = new Items());


    }
    private void InitData() {
        msgClassify.setAdapter(mTypeAdapter);
        msgcategory = getStrExtra(MESSAGECATEGORY);

    }

    private void loadCataData(String msgcategory) {
        UserManageDataApiStoreIml.getInstance().getAppMessageList(msgcategory)
                .subscribe(new NetWorkObserver<CateGoryMsg>() {
                    @Override
                    protected void onFail(String errormsg) {
                        if (refreshLayout!=null){
                            refreshLayout.finishRefresh();
                        }
                        shortToast(errormsg);
                    }

                    @Override
                    protected void onSuccess(CateGoryMsg result) {
                        if (refreshLayout!=null){
                            refreshLayout.finishRefresh();
                        }
                        if (!TextUtils.isEmpty(result.messageTitle)) {
                            toolbarTitle.setText(result.messageTitle);
                        }

                        if (result.messageList.size()==0){
                            multstateView.showView(MultStateView.STATUS_NOMSG);
                        } else {
                            multstateView.showView(MultStateView.STATUS_CONTENT);
                            showCateGoryData(result.messageList);
                        }
                    }
                });

    }

    private void showCateGoryData(List<AppMessageList> result) {
        for (AppMessageList message:result){
            mItems.add(TimeUtils.millis2String(Long.parseLong(message.sendTime)*1000,_FORMAT));
            mItems.add(message);
        }
        mTypeAdapter.notifyDataSetChanged();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_msg_notify;
    }


}
